<?php

$host = @$_POST['host'] ;
$username = @$_POST['username'] ;
$password = @$_POST['password'] ;
$databaseName  = @$_POST['databaseName'] ;
$journalid = @$_POST['journalid'] ;
$Year = @$_POST['year'] ;

$idphieu = @$_POST['idphieu'] ;
$soct = @$_POST['soct'] ;
$ngct = @$_POST['ngct'] ;
$hoten = @$_POST['hoten'] ;
$diachi = @$_POST['diachi'] ;
$lydo = @$_POST['lydo'] ;
$madv = @$_POST['madv'] ;
$solgctg = @$_POST['solgctg'] ;
$tygia = @$_POST['tygia'] ;
$matk = @$_POST['matk'] ;
$mant = @$_POST['mant'] ;
$mact = @$_POST['mact'] ;
$noco = @$_POST['noco'] ;

$tableArr = @$_POST['tableArr'] ;
$cautruc = @$_POST['cautruc'] ;


$codulieu = 0;
$succ = 1;
$mysql = array();
$i = 0;

foreach ($tableArr as $v) {
	$dulieu = ""; 
	$mysql[$i] = "" ;
	foreach ($v as $key=>$value) 
	{
		$dulieu .= "$value";
		$fdulieu = "$value";
		if ($fdulieu !== "")
		{		$mysql[$i] .= ", ". $cautruc[$key]."  = '".$value."'";
		}
	}
	if ($dulieu !== "")
	{
		$i++;
	}

}


if ($i==0)
{
	echo "2";
	return ;
}


$link = mysqli_connect($host,$username,$password,$databaseName) or die("Error " . mysqli_error($link));

if (!mysqli_set_charset($link, "utf8")) {
} else {
}


$mysqlQ = "DELETE FROM CTKTDT".$journalid. $Year. "  WHERE idPhieu='". $idphieu. "'";
$succ = mysqli_query($link, $mysqlQ);
if ($succ == false)
{
	echo "1";
	return;
}

$mysql_pre = "INSERT INTO CTKTDT".$journalid. $Year. "  SET idPhieu='". $idphieu. "'";
for ($dong = 0; $dong < $i ; $dong++) {
	$mysqlQ = $mysql_pre.  $mysql[$dong].";";
	echo $mysqlQ ;
	$succ = mysqli_query($link, $mysqlQ);
	if ($succ == false)
	{
		echo "1";
		return;
	}
}

$mysql_pa = "UPDATE CTKT".$journalid. $Year. "  SET ";
$mysql_pa .= "  soct='". $soct. "', ";
$mysql_pa .= "  ngct='". $ngct. "', ";
$mysql_pa .= "  hoten='". $hoten. "', ";
$mysql_pa .= "  diachi='". $diachi. "', ";
$mysql_pa .= "  lydo='". $lydo. "', ";
$mysql_pa .= "  solgctg='". $solgctg. "', ";
$mysql_pa .= "  tigia='". $tygia. "', ";
$mysql_pa .= "  tk='". $matk. "', ";
$mysql_pa .= "  mact='". $mact. "', ";
$mysql_pa .= "  mant='". $mant. "', ";
$mysql_pa .= "  noco='". $noco. "' ";
$mysql_pa .= "  where idPhieu = '".$idphieu."'";

echo $mysql_pa;
$succ = mysqli_query($link, $mysql_pa);
if ($succ == false)
{
	echo "1";
	return;
}


mysqli_close($link);
?> 

